package handler

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	"video_08_28/global"
	"video_08_28/internal/model"
	"video_08_28/internal/params"
	"video_08_28/pkg"
)

func RegisterUser(c *gin.Context) {
	var req params.RegisterUserForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	get := global.RDB.Get(context.Background(), "sendSms"+req.Mobile+"register")

	if get.Val() != req.Sms {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "短信验证码错误",
			"data":    "",
		})
		return
	}

	var user model.User

	user.Mobile = req.Mobile
	err := user.LoginUser()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "注册失败",
			"data":    err.Error(),
		})
		return
	}

	if user.Id != 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "手机号已注册",
			"data":    "",
		})
		return
	}

	user.Mobile = req.Mobile
	user.Password = pkg.Ma5(req.Password)
	err = user.RegisterUser()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "注册失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "登陆成功",
		"data":    "",
	})

}

func UpdateUserInfo(c *gin.Context) {
	var req params.UpdateUserInfoForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var info model.UserInfo

	info.UserId = req.UserId

	err := info.FindInfo()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "查询失败",
			"data":    "",
		})
		return
	}

	if info.Id == 0 {
		info = model.UserInfo{
			UserId: req.UserId,
			Name:   req.Name,
			Info:   req.Info,
		}

		err = info.AddInfo()
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code":    100000,
				"message": "添加失败",
				"data":    err.Error(),
			})
			return
		}

		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusOK,
			"message": "添加成功",
			"data":    info.Id,
		})
	}

	info = model.UserInfo{
		Id:     info.Id,
		UserId: req.UserId,
		Name:   req.Name,
		Info:   req.Info,
	}

	err = info.UpdatedInfo()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "修改失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "修改成功",
		"data":    info.Id,
	})
}

func GetUserInfo(c *gin.Context) {
	var req params.UpdateUserInfoForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	get := global.RDB.Get(context.Background(), fmt.Sprintf("userInfo:%d", req.UserId)).Val()

	var info model.UserInfo

	if get == "" {

		info.UserId = req.UserId
		err := info.FindInfo()
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code":    100000,
				"message": "查询失败",
				"data":    "",
			})
			return
		}

		marshal, err := json.Marshal(info)
		if err != nil {
			return
		}

		global.RDB.Set(context.Background(), fmt.Sprintf("userInfo:%d", req.UserId), marshal, time.Hour*24)

		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusOK,
			"message": "查询成功",
			"data":    info,
		})
	} else {
		json.Unmarshal([]byte(get), &info)

		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusOK,
			"message": "查询成功",
			"data":    get,
		})
	}

	rabbitmq := pkg.NewRabbitMQSimple("" +
		"kuteng")
	rabbitmq.PublishSimple(get)
	fmt.Println("发送成功！")
}
